﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Delonix_Regia_HMS.Classes;

namespace Delonix_Regia_HMS
{
    public partial class Login : Form
    {
        private int id = 0;
        private bool close = false;
        public string staffType { get; set; }

        
        public Login()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            int username;
            string password;
            if (string.IsNullOrEmpty(tbxStaffID.Text) || string.IsNullOrEmpty(tbxPassword.Text))
            {
                MessageBox.Show("You are missing a username or a password!");
            }
            else
            {
                username = Convert.ToInt32(tbxStaffID.Text);
                password = tbxPassword.Text;
                id = DBManager.Login(username, password);
                staffType = DBManager.sType(id);
                if (staffType.Equals("Administrator"))
                {
                    staffType = "Administrator";
                    close = true;
                    this.Close();
                }
                else if (staffType.Equals("Manager"))
                {
                    staffType = "Manager";
                    close = true;
                    this.Close();
                }
                else if (staffType.Equals("Receptionist"))
                {
                    staffType = "Receptionist";
                    close = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Login Unsuccessful");
                }

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            close = true;
            Application.Exit();
        }

        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!close)
            {
                e.Cancel = true;
            }   
        }

        private void restrictCharacter_KeyPress(object sender, KeyPressEventArgs e)
        {
            const char Delete = (char)8;
            e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
        }
        
    }
}
